package com.gxa.day2022_07_29_21.homework.order_system_4.service;

import com.gxa.day2022_07_29_21.homework.order_system_4.model.Dish;
import com.gxa.day2022_07_29_21.homework.order_system_4.model.Order;
import com.gxa.day2022_07_29_21.homework.order_system_4.dao.OrderDao;

import java.util.List;
import java.util.Scanner;

/**
 * @Description:
 * @Author: zhuiyi
 * @CreatTime: 2022/7/30 15:27
 **/
public class OrderService {
    private static int idx;
    private Scanner scanner = new Scanner(System.in);
    private OrderDao orderDao = new OrderDao();
    private DishService dishService = new DishService();

    /**
     * 保存订单
     *
     * @param order
     */
    public void saveOrder(Order order) {
        orderDao.saveOrder(order);
    }

    /**
     * 签收订单
     *
     * @param
     */
    public void signOrder() {
        List<Order> oders = orderDao.getOders();
//        boolean isSignFind = false;
        System.out.println("***签收订单***");
        printOrder();
        System.out.print("请选择要签收的订单序号：");
        int signOrderId = scanner.nextInt();

        if ((signOrderId - 1) > orderDao.getOders().size() - 1 || (signOrderId - 1) < 0) {//TODO 改为 自定义 异常
            throw new IllegalArgumentException("您选择的订单不存在！");
        }
        Order order = getOrderByIndex(signOrderId - 1);
        if (order.getOrderStatu() == 0 ) {
            orderDao.signOrder(signOrderId - 1);
            System.out.println("您的订单签收成功！");
            printOrder();
        } else if (order.getOrderStatu() == 1 ) {
            System.out.println("你的订单已经签收成功，无法再次签收！");
        }
    }


    /**
     * 打印订单
     */
    void printOrder() {
        List<Order> dishes = orderDao.getOders();
        System.out.println("序号\t订餐人\t订餐信息\t\t送餐时间\t送餐地址\t\t总金额\t订单状态");
        for (int i = 0; i < dishes.size(); i++) {
            Order order = dishes.get(i);
            if (order != null && order.getUserName() != null) {
                String statu = (order.getOrderStatu() == 0) ? "已预订" : "已完成";
                String time = order.getTime() + "点";
                String sumPrice = order.getSumPrice() + "元";
                System.out.println((i + 1) + "\t" + order.getUserName() + "\t\t" + order.getDishMsg() + "\t"
                        + time + "\t" + order.getAddress() + "\t" + sumPrice + "\t" + statu);
            }
        }
    }

    /**
     * 查看餐带
     */
    public void getOrders() {
        System.out.println("***查看餐带***");
        printOrder();
    }

    /**
     * 订餐，创建订单
     */
    public void createOrder() {
        List<Dish> dishes = dishService.getDishes();
        System.out.println("***我要订餐***");

        System.out.print("请输入订餐人姓名：");
        String name = scanner.next();
        dishService.printDishMenu();


        System.out.print("请输入菜品序号：");
        idx = scanner.nextInt();

        System.out.print("请输入您需要的份数：");
        int dishChooseNum = scanner.nextInt();

        System.out.print("请输入送餐时间：（送餐时间是：10点到20点 整点送餐）：");
        int time = scanner.nextInt();
        while (time < 10 || time > 20) {
            System.out.println("请输入10--20的整数：");
            time = scanner.nextInt();
        }
        System.out.print("请输入送餐地址：");
        String address = scanner.next();

        System.out.println("订餐成功");
//        idx = getDishIndex(dishChooseId);
        System.out.println("您的订单是：" + dishes.get(idx - 1) + " " + dishChooseNum + "份");
        System.out.println("送餐时间：" + time + "点");
        double sumPrice = dishChooseNum * dishes.get(idx - 1).getDishPrice();
        double deliverFee = sumPrice > 50 ? 0 : 5;

        System.out.println("餐费：" + sumPrice + "元，送餐费：" + deliverFee + "元，总计： " + (sumPrice + deliverFee) + "元");

        //存数据
        String disMsg = dishes.get(idx - 1).getDishName() + " " + dishChooseNum + "份";
        Order order = new Order(name, disMsg, time, address, sumPrice);
        saveOrder(order);

    }

    /**
     * 删除订单
     *
     * @param
     */
    public void deleteOrder() {
        System.out.println();
        System.out.println("***删除订单***");
        printOrder();
        System.out.print("请输入您要删除的订单序号：");

        int delId = scanner.nextInt();

        if ((delId - 1) > orderDao.getOders().size() - 1 || (delId - 1) < 0) {//TODO 改为 自定义 异常
            throw new IllegalArgumentException("删除失败，该订单不存在！");
        }
        Order order = getOrderByIndex(delId - 1);
//        if (order_system_4 == null) {
//            throw new IllegalArgumentException("删除失败，该订单不存在！");
//        }
        orderDao.deleteOrder(delId - 1);
        System.out.println("删除订单成功！");
        printOrder();
        System.out.println();

    }

    /**
     * 根据序号查询订单
     */
    public Order getOrderByIndex(int idx) {
        Order order = orderDao.getOrderByIndex(idx);
        return order;
    }

}
